#pragma once    
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include "inc/cppjieba/Jieba.hpp"
#include <mutex>


namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string& file_path,std::string* out)
        {
            // file_path ：路径；std::ios::in ： 只读
            std::fstream in(file_path,std::ios::in);

            if(!in.is_open()){
                std::cerr << "fstream open file fial " << std::endl;
                return false;
            }          

            std::string line;
            while(getline(in,line))
            {
                *out += line;
            }
            in.close();
            return true;
        }
    };


    
    class StringUtil
    {
    public:
        static void Split(const std::string& lien,std::vector<std::string>* out,const std::string& sep)
        {
            boost::split(*out, lien, boost::is_any_of(sep), boost::token_compress_on);
        }

    };

    const char* const DICT_PATH = "./dict/jieba.dict.utf8";
    const char* const HMM_PATH = "./dict/hmm_model.utf8";
    const char* const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char* const IDF_PATH = "./dict/idf.utf8";
    const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";

    class JiebaUtil
    {
    private:
        std::unordered_map<std::string,bool> stop_words;
    private:
        // 构建单例模式
        cppjieba::Jieba jieba;
        static JiebaUtil* instance;
        static std::mutex lock;
        // JiebaUtil(){} // "cppjieba::Jieba" 不存在默认构造函数
        JiebaUtil():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){}
    public:
        // 构建单例模式
        JiebaUtil(const JiebaUtil&)=delete;
        JiebaUtil& operator=(const JiebaUtil&) =delete;

        static JiebaUtil* getInstance()
        {
            if(instance == nullptr)
            {
                lock.lock();
                if(instance == nullptr)
                {
                    instance = new JiebaUtil();
                    instance->InitJiebaUtil();
                }
                lock.unlock();
            }
            return instance;
        }
        void InitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if(!in.is_open()){
                return;
            }

            std::string line;
            while(std::getline(in, line)){
                stop_words.insert({line, true});
            }

            in.close();
        }
    public:
        // 功能
        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
        {
            jieba.CutForSearch(src, *out);
            for(auto iter = out->begin(); iter != out->end(); ){
                auto it = stop_words.find(*iter);
                if(it != stop_words.end()){
                    // 说明当前的string 是暂停词，需要去掉
                    iter = out->erase(iter);
                }
                else{
                    iter++;
                }
            }
        }

        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            ns_util::JiebaUtil::getInstance()->CutStringHelper(src, out);
            //jieba.CutForSearch(src, *out);
        }

    };
    JiebaUtil* JiebaUtil::instance = nullptr;
    std::mutex JiebaUtil::lock;

    
    
}